SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROC [dbo].[SP_TASALD_EMPR_Q04]
/*---------------------------------------------------------------------*/
/*----- Empresa          : OFISIS S.A.                            -----*/
/*----- Cliente          : OFISIS S.A.                         	  -----*/
/*----- Sistema          : Contabilidad                           -----*/
/*----- Modulo           : Empresa                                -----*/
/*----- Programa         : Balance de sumas y saldos              -----*/
/*----- Nombre           : SP_TASALD_EMPR_Q04                	  -----*/
/*----- Script           : TASALD_EMPR                            -----*/
/*----- Desarrollado     : Franco Gamboni Koifman                 -----*/
/*----- Fecha            : 23/06/1997                             -----*/
/*----- Base de Datos    : MS SQL Server                          -----*/
/*----- Version BD       : 6.5                                    -----*/
/*----- Invoca a SP      :                                        -----*/
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/*----- Modificado 1 por : Martin Garcia Farfan                   -----*/
/*----- Fecha Modifica   : 22/09/2003                             -----*/
/*----- Detalle Modifica : PRUEBAS, ESTE PROCEDURE NO ES DE       -----*/
/*-----                    LA VERSION 5                           -----*/
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/*----- Drop Proc SP_TASALD_EMPR_Q04                              -----*/
/*----- Grant Execute On SP_TASALD_EMPR_Q04 To PUBLIC             -----*/
/*----- SP_TASALD_EMPR_Q04 '01', 1, 1999, 3, 2, 5                 -----*/
/*---------------------------------------------------------------------*/
@ISCO_EMPR	TD_VC_002,
@INNU_CNTB_EMPR TD_IN_001, 
@INNU_ANNO	TD_IN_001,
@INNU_MESE	TD_IN_001,
@INNU_DGTO	TD_IN_001,
@INNU_QUIE      TD_IN_001 
As
                                                                                                            
Declare                                                                                                                 
 @CSCO_CNTA	 TD_VC_010,
 @CSNO_CNTA	 TD_VC_050,
 @CNIM_DEBE	 TD_NU_016_004,
 @CNIM_HABE	 TD_NU_016_004,
 @VNIM_DEBE_ACUM TD_NU_016_004,
 @VNIM_HABE_ACUM TD_NU_016_004,
 @VSDE_CMDO	 Char(255) 
                                                                                                                                                  
Create Table #TWSALD_EMPR_Q04_1
(CO_CNTA_EMPR 	   Char(10)    NULL,
 DE_CNTA_EMPR 	   Varchar(50) NULL,
 IM_MVTO_DEBE	   Numeric(16,4)       NULL,
 IM_MVTO_HABE 	   Numeric(16,4)       NULL,  
 IM_MVTO_DEBE_ACUM  Numeric(16,4)       NULL,  
 IM_MVTO_HABE_ACUM  Numeric(16,4)       NULL,  CO_CNTA       	   Char(02)    NULL)
                                   
If @INNU_DGTO != 0
 Begin
  Declare CU_TASALD_EMPR_Q04_1 Cursor
  For
   Select 
          CUENTA = T1.CO_CNTA_EMPR, NOMBRE = MAX(RTRIM(T2.DE_CNTA_EMPR)),
          CARGO_SOLES = SUM(ROUND(ISNULL(T1.IM_CARG_CNTB,0),2)),            
          ABONO_SOLES = SUM(ROUND(ISNULL(T1.IM_ABON_CNTB,0),2))
   From TASALD_EMPR T1, TMPLAN_EMPR T2
   Where	T1.CO_EMPR 	= @ISCO_EMPR  	And
		T1.NU_CNTB_EMPR = @INNU_CNTB_EMPR And
		T1.NU_ANNO	= @INNU_ANNO 	And
		T1.NU_MESE	= @INNU_MESE 	And
		DATALENGTH(RTRIM(T1.CO_CNTA_EMPR)) = @INNU_DGTO And
		T1.TI_AUXI_EMPR IS NULL 	 	And		       
		T1.CO_AUXI_EMPR IS NULL 	 	And		       
		T1.CO_ORDE_SERV IS NULL 	 	And		       
		T1.CO_EMPR 	= T2.CO_EMPR 	And
		T1.CO_CNTA_EMPR = T2.CO_CNTA_EMPR
   Group By T1.CO_CNTA_EMPR
 End

If @INNU_DGTO = 0
 Begin
  Declare CU_TASALD_EMPR_Q04_1 Cursor
  For
   Select 
          CUENTA = T1.CO_CNTA_EMPR, NOMBRE = MAX(RTRIM(T2.DE_CNTA_EMPR)),
          CARGO_SOLES = SUM(ROUND(ISNULL(T1.IM_CARG_CNTB,0),2)), 
          ABONO_SOLES = SUM(ROUND(ISNULL(T1.IM_ABON_CNTB,0),2))
   From TASALD_EMPR T1, TMPLAN_EMPR T2
   Where	T1.CO_EMPR 	= @ISCO_EMPR  	And
		T1.NU_CNTB_EMPR = @INNU_CNTB_EMPR And
		T1.NU_ANNO	= @INNU_ANNO 	And
		T1.NU_MESE	= @INNU_MESE 	And
		T1.TI_AUXI_EMPR IS NULL 	 	And		       
		T1.CO_AUXI_EMPR IS NULL 	 	And		       
		T1.CO_ORDE_SERV IS NULL 	 	And		       
		T1.CO_EMPR 	= T2.CO_EMPR 	And
		T1.CO_CNTA_EMPR = T2.CO_CNTA_EMPR
   Group By T1.CO_CNTA_EMPR
   Order By T1.CO_CNTA_EMPR
 End

                                  
   Open CU_TASALD_EMPR_Q04_1
   Fetch CU_TASALD_EMPR_Q04_1 Into @CSCO_CNTA, @CSNO_CNTA, @CNIM_DEBE, @CNIM_HABE
                                                                      
   While @@Fetch_status = 0
	Begin
	  Select @VNIM_DEBE_ACUM  = SUM(ROUND(T1.IM_CARG_CNTB,2)), 
		 @VNIM_HABE_ACUM = SUM(ROUND(T1.IM_ABON_CNTB,2))
	  From  TASALD_EMPR T1, TMPLAN_EMPR T2
	  Where T1.CO_EMPR 	= @ISCO_EMPR 	 And
		T1.NU_CNTB_EMPR = @INNU_CNTB_EMPR  And
		T1.NU_ANNO	= @INNU_ANNO 	 And
		T1.NU_MESE	Between 0 And @INNU_MESE And
		T1.CO_CNTA_EMPR = RTRIM(@CSCO_CNTA) And
		T1.TI_AUXI_EMPR IS NULL 		 And
		T1.CO_AUXI_EMPR IS NULL 	 	And		       
		T1.CO_ORDE_SERV IS NULL 	 	And		       
		T1.CO_EMPR 	= T2.CO_EMPR 	 And
		T1.CO_CNTA_EMPR = T2.CO_CNTA_EMPR
	Group By T1.CO_CNTA_EMPR
                                                                             
	Insert #TWSALD_EMPR_Q04_1(CO_CNTA_EMPR, DE_CNTA_EMPR, IM_MVTO_DEBE, IM_MVTO_HABE, 
			 IM_MVTO_DEBE_ACUM, IM_MVTO_HABE_ACUM, CO_CNTA ) 
	Values ( RTRIM(@CSCO_CNTA),RTRIM(@CSNO_CNTA),@CNIM_DEBE,@CNIM_HABE,
 		 @VNIM_DEBE_ACUM,@VNIM_HABE_ACUM, SUBSTRING(@CSCO_CNTA,1,@INNU_QUIE) )
	
      Fetch CU_TASALD_EMPR_Q04_1 Into @CSCO_CNTA, @CSNO_CNTA, @CNIM_DEBE, @CNIM_HABE
   End
Close CU_TASALD_EMPR_Q04_1
Deallocate CU_TASALD_EMPR_Q04_1
                                  
Select CO_CNTA_EMPR, Substring(DE_CNTA_EMPR,1,50), Round(IM_MVTO_DEBE,2), Round(IM_MVTO_HABE,2), 
Round(IM_MVTO_DEBE_ACUM,2), Round(IM_MVTO_HABE_ACUM,2), CO_CNTA
From #TWSALD_EMPR_Q04_1
Order by CO_CNTA_EMPR

return                                                                            
/*------------------------------ Fin ------------------------------------------*/


GO
